package ar.fiuba.tp7552.buildadmin.webactions.users;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

import org.apache.commons.lang.StringUtils;

import ar.fiuba.tp7552.buildadmin.model.BusinessException;
import ar.fiuba.tp7552.buildadmin.model.auth.Role;
import ar.fiuba.tp7552.buildadmin.model.auth.User;
import ar.fiuba.tp7552.buildadmin.service.UserService;
import ar.fiuba.tp7552.buildadmin.util.Cipher;
import ar.fiuba.tp7552.buildadmin.webactions.BaseAction;

import com.opensymphony.xwork2.Preparable;

public class UserAction extends BaseAction implements Preparable {
	
	private UserService userService;
	
	private User user = new User();
	private String password;
	private String password2;
	private Role selectedRoles[];
	private Collection<Role> availableRoles = new HashSet<Role>();

	// ~~ Constructor ====================================================
	
	public UserAction(UserService userService) {
		super();
		this.userService = userService;
	}
	
	// ~~ Actions ========================================================
	
	/**
	 * Obtiene de la db el usuario si corresponde.
	 */
	@Override
	public void prepare() {
		if (user != null && user.getId() != null) {
			user = userService.getUser(user.getId());
		}
	}
	
	/**
	 * Carga un usuario para editarlo
	 * (el usuario se carga con el metodo prepare)
	 * @return
	 */
	public String load() {
		return BaseAction.SUCCESS;
	}
	
	/**
	 * Crea un nuevo usuario validando que no se repita
	 * el nombre de usuario.
	 * @return
	 */
	public String save() {
		
		Collection<Role> roles = new HashSet<Role>(Arrays.asList(selectedRoles));
			
		if (user.isNew()) {
			user.setPassword(Cipher.encrypt(password));
			user.setRoles(roles);
			try {
				userService.createUser(user);
			} catch (BusinessException e) {
				this.addFieldError("username", e.getMessage());
				return BaseAction.INPUT;// vuelvo al formulario para mostrar el error.
			}
		} else {
			if (StringUtils.isNotEmpty(password)) {
				user.setPassword(Cipher.encrypt(password));
			}
			user.getRoles().retainAll(roles);//elimino loss roles que no tiene
			user.getRoles().addAll(roles);// agrego los que falten.
			userService.getAll();
		}
		return BaseAction.SUCCESS;
	}
	
	public String delete() {
		userService.deleteUser(user.getId());
		return SUCCESS;
	}

	
	// ~~ getters y setters ========================================================
	
	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}

	public Role[] getSelectedRoles() {
		return selectedRoles;
	}

	public void setSelectedRoles(Role[] selectedRoles) {
		this.selectedRoles = selectedRoles;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public Collection<Role> getAvailableRoles() {
		if (availableRoles.isEmpty()) {
			// FIXME sacar de la db
			Role role1 = new Role();	
			role1.setId(1L);
			role1.setDescription("Administrador");
		
			Role role2 = new Role();
			role2.setId(2L);
			role2.setDescription("Responsable de gastos");
			
			Role role3 = new Role();
			role3.setId(3L);
			role3.setDescription("Responsable de cobros");
			
			Role role4 = new Role();
			role4.setId(4L);
			role4.setDescription("Responsable de edificios");

			availableRoles.add(role1);
			availableRoles.add(role2);
			availableRoles.add(role3);
			availableRoles.add(role4);
		}
		availableRoles.removeAll(user.getRoles());
		return availableRoles;
	}

}
